package com.useeinfo.oa.modules.base.biz;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.dao.AuditNodeDao;
import com.useeinfo.oa.modules.base.entity.AuditNode;
import com.useeinfo.oa.modules.base.entity.Personnel;
import com.useeinfo.oa.modules.security.biz.OrganizationBiz;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/7 15:50
 * @Description:
 */
@Service
public class AuditNodeBiz extends CrudBiz<AuditNodeDao, AuditNode> {
    @Autowired
    private AuditFlowBiz auditFlowBiz;
    @Autowired
    private PositionBiz positionBiz;
    @Autowired
    private OrganizationBiz organizationBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;

    //保存审批节点
    public ResultDto saveNode(AuditNode auditNode, String parent, String flowId) {
        auditNode.setAuditFlow(auditFlowBiz.findModel(StringConverters.null2Long(flowId)));
        if (auditNode.getId() != null) {
            dao.update(auditNode);
        } else {
            dao.add(auditNode);
            delayAfterNode(parent, auditNode);
        }
        //保存完整审批节点
        auditFlowBiz.saveAllTree(flowId);
        return ResultUtil.success();
    }

    //获取审批流的第一个节点
    public AuditNode getFirstNode(String flowId) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("flowId", flowId);
        queryParam.getSqlMap().put("parentId", "-1");
        List<AuditNode> auditNodeList = dao.findList(queryParam);
        if (CollectionUtils.isNotEmpty(auditNodeList)) {
            return auditNodeList.get(0);
        } else {
            return null;
        }
    }

    //获取节点tree
    public List<AuditNode> orderByList(String flowId) {
        AuditNode auditNode = getFirstNode(flowId);
        List<AuditNode> auditNodes = new ArrayList<>();
        if (auditNode != null) {
            auditNodes.add(auditNode);
            while (getNodeByParentId(auditNode.getId(), flowId) != null) {
                auditNodes.add(getNodeByParentId(auditNode.getId(), flowId));
                auditNode = getNodeByParentId(auditNode.getId(), flowId);
            }
        }
        return auditNodes;

    }

    //根据parentId获取该节点的数据
    public AuditNode getNodeByParentId(Long parentId, String flowId) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("parentId", parentId + "");
        queryParam.getSqlMap().put("flowId", flowId);
        List<AuditNode> auditNodeList = dao.findList(queryParam);
        if (CollectionUtils.isNotEmpty(auditNodeList) && auditNodeList.size() == 1) {
            return auditNodeList.get(0);
        } else {
            return null;
        }
    }

    public JSONObject getNodeJSONByParentId(Long parentId, String flowId){
        JSONObject jsonObject = new JSONObject();
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("parentId", parentId + "");
        queryParam.getSqlMap().put("flowId", flowId);
        List<AuditNode> auditNodeList = dao.findList(queryParam);
        if (CollectionUtils.isNotEmpty(auditNodeList) && auditNodeList.size() == 1) {
            AuditNode auditNode = auditNodeList.get(0);
            jsonObject.put("organizationId",auditNode.getOrganizationId());
            jsonObject.put("nodeTypeEnum",auditNode.getNodeTypeEnum().getCode());
            jsonObject.put("relationId",auditNode.getRelationId());
            return jsonObject;
        } else {
            return null;
        }
    }

    //后一个节点顺延
    public void delayAfterNode(String parentId, AuditNode auditNode) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("parentId", parentId);
        queryParam.getSqlMap().put("flowId", auditNode.getAuditFlow().getId() + "");
        //后面一个节点顺延
        List<AuditNode> auditNodeList = dao.findList(queryParam);
        if (CollectionUtils.isNotEmpty(auditNodeList)) {
            for (AuditNode auditNode1 : auditNodeList) {
                if (auditNode1.getId() != auditNode.getId()) {
                    auditNode1.setParentId(auditNode.getId());
                    dao.update(auditNode1);
                }
            }
        }
    }

    //后一个节点往前
    public void pushAfterNode(AuditNode auditNode) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("parentId", auditNode.getId() + "");
        queryParam.getSqlMap().put("flowId", auditNode.getAuditFlow().getId() + "");
        //后面一个节点顺延
        List<AuditNode> auditNodeList = dao.findList(queryParam);
        if (CollectionUtils.isNotEmpty(auditNodeList)) {
            for (AuditNode auditNode1 : auditNodeList) {
                auditNode1.setParentId(auditNode.getParentId());
                dao.update(auditNode1);
            }
        }
    }

    public JSONObject getNodeJson(AuditNode auditNode) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", auditNode.getId());
        jsonObject.put("name", auditNode.getNodeName());
        jsonObject.put("nodeType", auditNode.getNodeTypeEnum().getName());
        if (auditNode.getNodeTypeEnum().getCode().equals("POSITION")) {
            jsonObject.put("organizationName", organizationBiz.findModel(StringConverters.null2Long(auditNode.getOrganizationId())).getOrganizationName());
            jsonObject.put("positionName", positionBiz.findModel(StringConverters.null2Long(auditNode.getRelationId())).getName());
        } else if (auditNode.getNodeTypeEnum().getCode().equals("USER")){
            jsonObject.put("organizationName", organizationBiz.findModel(StringConverters.null2Long(auditNode.getOrganizationId())).getOrganizationName());
            jsonObject.put("userName", systemUserBiz.findModel(StringConverters.null2Long(auditNode.getRelationId())).getUserName());
        }
        jsonObject.put("parentName", dao.findModel(StringConverters.null2Long(auditNode.getParentId())) == null ? "" : dao.findModel(StringConverters.null2Long(auditNode.getParentId())).getNodeName());

        jsonObject.put("createDate", DateTimeUtils.formatDateToStringWithTime(auditNode.getCreateDate()));
        return jsonObject;
    }
}
